package com.ym.b2b.product.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.product.dao.EmBProductTypeDAO;
import com.ym.b2b.product.service.EmBProductTypeService;
import com.ym.b2b.product.service.ProductService;
import com.ym.b2b.product.sqlbean.EmBProductType;
import com.zc.commons.base.Result;

/**
 * @ClassName: ManagerCategoryController
 * @Description: TODO
 * @Author 竺夏军
 * @Date 2014年12月31日 上午11:29:18
 * @Modify
 * @CopyRight 信息化团队
 */
@Controller
@RequestMapping("/product")
public class ManagerCategoryController extends BaseController {

    @Autowired
    private ProductService        productService;

    @Autowired
    private EmBProductTypeDAO     emBProductTypeDAO;

    @Autowired
    private EmBProductTypeService emBProductTypeService;

    /**
     * 返回RESULT 后台出BUG...改为返回list
     * @Title: searchEmBProductType 
     * @Modify cym
     */
    @RequestMapping("searchEmBProductType.do")
    @ResponseBody
    public List<EmBProductType> searchEmBProductType(EmBProductType emBProductType, HttpServletRequest req) {
        try {
            List<EmBProductType> list = emBProductTypeDAO.searchEmBProductType(emBProductType);
            if (null != emBProductType.getParentTypeId() && !"".equals(emBProductType.getParentTypeId())) {
                // 检查是否为根目录
                Long a = emBProductTypeDAO.hasTheRootEmBProductType(emBProductType);
                if (a == 0) {
                    for (EmBProductType t : list) {
                        t.setState("");
                    }
                }
            }
            return list;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取商品分类列表 必须输入展示类型
     */
    @RequestMapping("showEmBproductTypeList.do")
    @ResponseBody
    public Object showEmBproductTypeList(EmBProductType emBProductType, HttpServletRequest req) {
        return emBProductTypeService.showEmBproductTypeList(emBProductType);
    }

    /**
     * 分类明细
     * 
     * @Title: showEmBproductTypeDetail
     * @Description: TODO
     * @Param @param emBProductType
     * @Param @param req
     * @Param @return
     * @Return EmBProductType
     * @Throws
     */
    @RequestMapping("showEmBproductTypeDetail.do")
    @ResponseBody
    public EmBProductType showEmBproductTypeDetail(EmBProductType emBProductType, HttpServletRequest req) {
        if (null == emBProductType.getProductTypeId()) {
            return null;
        }
        return emBProductTypeDAO.showEmBProductTypeDetail(emBProductType);
    }

    @RequestMapping("saveOrUpdateEmBProductType.do")
    @ResponseBody
    public Result saveOrUpdateEmBProductType(EmBProductType emBProductType, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emBProductTypeService.saveOrUpdateEmBProductType(emBProductType);
            return result;
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("deleteEmBProductType.do")
    @ResponseBody
    public Result deleteEmBProductType(EmBProductType emBProductType, HttpServletRequest req) {
        Result result = new Result();
        try {
            emBProductTypeService.deleteEmBProductType(emBProductType);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

}
